package com.meirimoney.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.meirimoney.app.R;
import com.meirimoney.sdk.models.Commodity;

/**
 * 商品列表适配器
 * 
 * @author gewei
 * 
 */
public class CommodityAdapter extends BaseAdapter
{

	private static Map<String, Integer> imgMap;

	public static Map<String, Integer> getImgMap()
	{
		if (null == CommodityAdapter.imgMap)
		{
			imgMap = new HashMap<String, Integer>();
			imgMap.put("话费充值卡10元", R.drawable.mrm_huafei10);
			imgMap.put("支付宝提现5元", R.drawable.mrm_alipay5);
			imgMap.put("话费充值卡20元", R.drawable.mrm_huafei20);
			imgMap.put("话费充值卡50元", R.drawable.mrm_huafei50);
			imgMap.put("支付宝提现10元", R.drawable.mrm_alipay10);
			imgMap.put("支付宝提现20元", R.drawable.mrm_alipay20);
		}
		return CommodityAdapter.imgMap;
	}

	/***
	 * 初始化商品列表适配器
	 * 
	 * @param commodities
	 *            商品列表
	 * @param context
	 *            当前执行上下文
	 */
	public CommodityAdapter(Context context, List<Commodity> commodities)
	{
		this.commodities = commodities;
		this.context = context;
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return this.commodities.size();
	}

	@Override
	public Object getItem(int position)
	{
		return this.commodities.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ItemViewHold viewHold;
		if (null == convertView)
		{
			// 初始当前View面板
			convertView = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.mrm_commodity_item, null);
			viewHold = new ItemViewHold(
					(ImageView) convertView.findViewById(R.id.commodityImg),
					(TextView) convertView.findViewById(R.id.commodityTitle),
					(TextView) convertView.findViewById(R.id.commodityIntegral));
			convertView.setTag(viewHold);
		} else
		{
			viewHold = (ItemViewHold) convertView.getTag();
		}
		viewHold.setView(this.commodities.get(position));
		return convertView;
	}

	/***
	 * 当前执行的上下文
	 */
	private Context context;

	/***
	 * 商品列表
	 */
	private List<Commodity> commodities;

	/***
	 * 列表项的持有对象
	 * 
	 * @author gewei
	 * 
	 */
	class ItemViewHold
	{

		/***
		 * 初始化商品View对象
		 * 
		 * @param context
		 *            当前上下文
		 */
		public ItemViewHold(ImageView img, TextView title, TextView integral)
		{
			this.commodityImg = img;
			this.commodityTitle = title;
			this.commodityIntegral = integral;
		}

		/***
		 * 返回商品项目
		 * 
		 * @param commodity
		 *            商品项
		 */
		public void setView(Commodity commodity)
		{
			Integer resource = CommodityAdapter.getImgMap().get(commodity.Name);
			commodityImg.setImageResource(resource);
			commodityTitle.setText(commodity.Name);
			commodityIntegral.setText(Html
					.fromHtml("需要<font color=\"#ff6600\">" + commodity.Integral
							+ "</font>积分"));
		}

		/***
		 * 获取商品的图片信息
		 * 
		 * @return
		 */
		public ImageView getImg()
		{
			return commodityImg;
		}

		/***
		 * 获取商品的标题
		 * 
		 * @return
		 */
		public TextView getTitle()
		{
			return commodityTitle;
		}

		/***
		 * 获取商品的积分信息
		 * 
		 * @return
		 */
		public TextView getIntegral()
		{
			return commodityIntegral;
		}

		/***
		 * 商品的图片信息
		 */
		private ImageView commodityImg;

		/***
		 * 商品的积分信息
		 */
		private TextView commodityTitle;

		/***
		 * 商品需要的积分信息
		 */
		private TextView commodityIntegral;
	}
}
